package br.com.cloudstorage;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public final class Monitor {

	private static Monitor instance = null;
	public static final Logger logger = Logger.getLogger("RateBackupLog");
	private List<String> filesToBackup;

	private Monitor() {
		this.filesToBackup = new ArrayList<String>();
	}

	public void writeBackupData(HashMap<String, String> backupData) {
		try {
			ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
			ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);

			objectStream.writeObject(backupData);
			objectStream.flush();
			objectStream.close();

			Monitor.logger.info("Path_Id: " + backupData.get("path_id") + " Start_Time: "
					+ backupData.get("start_time") + " End_Time: " + backupData.get("end_time") + " NumberOfPeer: "
					+ backupData.get("numberOfPeer") + " FileLength: " + backupData.get("fileLength"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Monitor getInstance() {
		if (Monitor.instance == null) {
			Monitor.prepareLogger();
			Monitor.instance = new Monitor();
		}
		return Monitor.instance;
	}

	private static void prepareLogger() {
		try {
			FileHandler fh = new FileHandler("rateBackupLogger.log");
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			Monitor.logger.addHandler(fh);
			Monitor.logger.setUseParentHandlers(false); // inibe a
																// exibicao do
																// log no
																// console
			// logger.setLevel(Level.ALL);
			// logger.setLevel(Level.FINEST);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
